#!/usr/bin/env python

__author__ = 'blogporter@gmail.com'

import blogprovider

class BlogProviderType:
    """
    Enumerate supported BlogProvider types
    """
    BLOGGER = 1
    WORDPRESS = 2
    CSDN = 3
    LIVESPACE = 4
    CNBLOGS = 5
    SINA = 6
    NETEASE = 7

class UnsupportedBlogError(Exception):
    def __init__(self, t):
        self.t = t

    def __str__(self):
        return "%d is not a supported blog type"%self.t

class BlogProviderFactory:
    """
    Utility factory class for creating BlogProvider instance
    """
    def __init__(self):
        pass

    def GetBlogProvider(self, blogtype, user_name, password, file_path=None):
        """
        Retrieve a BlogProvider instance
            blogtype: BlogProviderType.[type]
            user_name & password: credential for logging in the blog 
        
        (Factory method pattern)
        """
        if blogtype == BlogProviderType.BLOGGER:
            # import blogger here because blogger depends on a 3rd party lib: gdata
            # and it may not have been installed
            # we defer the importing of it so that applications make use of it at least run
            try:
                import blogger
                return blogger.BloggerProvider(user_name, password)
            except ImportError as err:
                raise ImportError(err, "you must install gdata (http://code.google.com/p/gdata-python-client/) to work against blogger/blogspot")
        elif blogtype == BlogProviderType.WORDPRESS:
            return blogprovider.WordpressProvider(user_name, password)
        elif blogtype == BlogProviderType.CSDN:
            raise UnsupportedBlogError(BlogProviderType.CSDN)
            return blogprovider.CsdnProvider(user_name, password)
        elif blogtype == BlogProviderType.CNBLOGS:
            return blogprovider.CnblogsProvider(user_name, password)
        elif blogtype == BlogProviderType.SINA:
            return blogprovider.SinaProvider(user_name, password)
        elif blogtype == BlogProviderType.NETEASE:
            return blogprovider.NeteaseProvider(user_name, password)
        elif blogtype == BlogProviderType.LIVESPACE:
            if not file_path:
                raise ValueError("must supply file_path argument to instantiate LiveSpaceProvider")
            import livespaceprovider 
            return livespaceprovider.LiveSpaceProvider(file_path)

